<template>
  <div>
    <el-row :gutter="20">
      <el-form :model="searchLsit" ref="searchLsit" size="normal">
        <el-col :span="8" :offset="0">
          <el-form-item label="">
            <el-input v-model="searchLsit.code" clearable placeholder="账号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="0">
          <el-form-item label="">
            <el-input v-model="searchLsit.name" clearable placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <el-col :span="8" :offset="0">
        <div class="right">
          <el-button type="primary" size="default" @click="search" :loading="loading">查询</el-button>
          <el-button type="primary" size="default" @click="onEdits" :loading="loading">批量更新</el-button>
        </div>
      </el-col>
    </el-row>

    <el-table :data="tebleData" border stripe v-loading="loading" :height="offsetHeight" ref="tableData">
      <el-table-column type="selection" width="50" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column prop="code" label="单位账号" width="" align="center">
      </el-table-column>
      <el-table-column prop="name" label="单位名称" width="" align="center">
      </el-table-column>
      <el-table-column label="操作" width="100" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="edit(row.code)" :loading="saveLoading">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="机构设置" :visible.sync="show" width="30%" @close="cancel">
      <el-form :model="unitList" ref="unitList" label-width="100px" size="normal">
        <el-form-item label="新增截止时间">
          <el-date-picker v-model="unitList.info_add_flag_time" type="date" value-format="yyyy-MM-dd"
            placeholder="选择日期时间" class="wid">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改截止时间">
          <el-date-picker v-model="unitList.info_edit_flag_time" type="date" value-format="yyyy-MM-dd"
            placeholder="选择日期时间" class="wid">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="机构权限">
          <el-select v-model="unitList.company_type" value-key="company_type" placeholder="" class="wid" clearable>
            <el-option v-for="item in companyType" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button type="danger" @click="cancel">取消</el-button>
        <el-button type="primary" @click="onConfirm" :loading="loading" v-if="unitList.company_code">更新</el-button>
        <el-button type="primary" @click="onConfirms" :loading="loading" v-else>批量更新</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { d_companyType } from '@/utils/dicts'
export default {
  name: 'PartUnit',
  components: {

  },
  props: {

  },
  data () {
    return {
      offsetHeight: document.documentElement.clientHeight - 280,
      loading: false,
      saveLoading: false,
      searchLsit: {},
      tebleData: [],
      show: false,
      unitList: {},
      companyType: d_companyType
    }
  },
  created () {

  },
  mounted () {
    this.init()
  },
  methods: {
    async init () {
      this.loading = true
      const res = await this.$http('sysUnitList', {
        ...this.searchLsit
      })
      this.tebleData = res
      this.loading = false
    },
    search () {
      this.init()
      this.saveLoading = false
    },
    onAdd () {

    },
    async edit (code) {
      this.saveLoading = true
      const res = await this.$http('sysUnitObj', {
        company_code: code
      })
      this.unitList = {
        company_code: res.company_code,
        info_add_flag_time: this.$util.formatDate(res.info_add_flag_time),
        info_edit_flag_time: this.$util.formatDate(res.info_edit_flag_time),
        company_type: res.company_type
        // delete_flag_time: this.$util.formatDate(res.delete_flag_time),
      }
      this.show = true
      this.saveLoading = false
    },
    onEdits () {

      this.unitList = {}
      this.show = true
    },
    async onConfirm () {
      let ids = []
      ids.push(this.unitList.company_code)
      const res = await this.$http('sysUnitUp', {
        ids: ids,
        ...this.unitList
      })
      this.$tips.success(res.errmsg)
      this.show = false
    },
    async onConfirms () {
      let ids = []
      if (this.$refs.tableData.selection == 0) {
        this.$tips.error('请选择至少一个')
        return false
      } else {
        this.$refs.tableData.selection.forEach(item => {
          ids.push(item.code)
        })
      }
      const res = await this.$http('sysUnitUp', {
        ids: ids,
        ...this.unitList
      })
      this.$tips.success(res.errmsg)
      this.show = false
    },
    cancel () {
      this.show = false
    },
  },
  computed: {

  }
}
</script>
<style lang='scss' scoped>
</style>
